<!-- 短信验证码登录页 -->
<template>
	<view class="uni-content">
		<uni-forms>
			<uni-id-pages-sms-form focusCaptchaInput v-model="code" type="login-by-sms" ref="smsCode" :phone="phone">
			</uni-id-pages-sms-form>
		</uni-forms>
		<!-- 顶部文字 -->
		<text class="tip"  v-show="false">请输入邀请码(默认为L7HEWV)</text>
		<uni-easyinput  type="text" class="input-box" :inputBorder="false" placeholder="请输入邀请码" @change="onSearch"  v-model="searchVal" v-show="false">
		</uni-easyinput>
		<button class="uni-btn send-btn" type="primary" @click="submit">登录</button>
		<uni-popup-captcha @confirm="submit" v-model="captcha" scene="login-by-sms" ref="popup"></uni-popup-captcha>
	</view>
</template>
<script>
	import mixin from '@/subunimodules/uni-id-pages/common/login-page.mixin.js';
	
	import cloudImage from '@/subunimodules/uni-id-pages/components/cloud-image/cloud-image.vue';
	import uniIdPagesAgreements from '@/subunimodules/uni-id-pages/components/uni-id-pages-agreements/uni-id-pages-agreements.vue';
	import uniIdPagesAvatar from '@/subunimodules/uni-id-pages/components/uni-id-pages-avatar/uni-id-pages-avatar.vue';
	import uniIdPagesBindMobile from '@/subunimodules/uni-id-pages/components/uni-id-pages-bind-mobile/uni-id-pages-bind-mobile.vue';
	import uniIdPagesFabLogin from '@/subunimodules/uni-id-pages/components/uni-id-pages-fab-login/uni-id-pages-fab-login.vue';
	import uniIdPagesInvForm from '@/subunimodules/uni-id-pages/components/uni-id-pages-inv-form/uni-id-pages-inv-form.vue';
	import uniIdPagesSmsForm from '@/subunimodules/uni-id-pages/components/uni-id-pages-sms-form/uni-id-pages-sms-form.vue';
	import uniIdPagesUserProfile from '@/subunimodules/uni-id-pages/components/uni-id-pages-user-profile/uni-id-pages-user-profile.vue';
	export default {
		components: {
			cloudImage,
			uniIdPagesAgreements,
			uniIdPagesAvatar,
			uniIdPagesBindMobile,
			uniIdPagesFabLogin,
			uniIdPagesInvForm,
			uniIdPagesSmsForm,
			uniIdPagesUserProfile
		},
		mixins: [mixin],
		data() {
			return {
				"code": "",
				"phone": "",
				"captcha": "",
				"invCode": "",
				searchVal:''

			}
		},
		computed: {
			tipText() {
				return '验证码已通过短信发送至' + this.phone;
			},
		},
		onLoad({
			phoneNumber
		}) {
			this.phone = phoneNumber;
		},
		onShow() {
			// #ifdef H5
			document.onkeydown = event => {
				var e = event || window.event;
				if (e && e.keyCode == 13) { //回车键的键值为13
					this.submit()
				}
			};
			// #endif
		},
		methods: {
			// 键盘搜索,输入搜索
			onSearch() {
				this.invCode = this.searchVal;
				//console.info(JSON.stringify(this.invCode))
			},
			submit() { //完成并提交
				const uniIdCo = uniCloud.importObject("uni-id-co", {
					errorOptions: {
						type: 'toast'
					}
				})
				if (this.code.length != 6) {
					this.$refs.smsCode.focusSmsCodeInput = true
					return uni.showToast({
						title: '验证码不能为空',
						icon: 'none'
					});
				}
				if(this.invCode ==0){
					this.invCode = "L7HEWV"
				}
				var that = this
				that.$http('user.smsLogin', {
					phone: this.phone,
					code: this.code,
					invCode: this.invCode,
				}).then(res => {
					 
					if (res.code === 1) {
					    uni.setStorageSync("UId", res.user.id);
						// uni.setStorageSync("UId",5356);
						uni.setStorageSync("opents", 1)
						uni.setStorageSync("mobile", res.user.mobile)
						uni.setStorageSync("isLogin", true)
						uni.setStorageSync("InvitationCode", res.InvitationCode)
						this.$http('user.getToken',{
							uid: res.user.id
						}).then(res=>{
							uni.setStorageSync("token", res.data.token)
							uni.navigateBack({
								delta: 2
							})
						})
					}
					else{
						uni.showToast({
							title:"请输入正确的验证码",
							icon:null
						})
					}
					 
				});


			}
		}
	}
</script>
<style scoped lang="scss">
	@import "@/subunimodules/uni-id-pages/common/login-page.scss";

	.tip {}

	.popup-captcha {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		padding: 20rpx;
		background-color: #FFF;
		border-radius: 2px;
		flex-direction: column;
		position: relative;
	}

	.popup-captcha .title {
		font-weight: normal;
		padding: 0;
		padding-bottom: 15px;
		color: #666;
	}

	.popup-captcha .close {
		position: absolute;
		bottom: -40px;
		margin-left: -13px;
		left: 50%;
	}

	.popup-captcha .uni-btn {
		margin: 0;
	}
</style>
